﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Core.Model;
using Core.Repository;

namespace Data
{
    public class Repository<TObject> : IRepository<Guid, TObject> where TObject : EntityBase<Guid>, new()
    {
        protected readonly DbContext Dbx;
        public Repository(IDbContextFactory dbFactory)
        {
            Dbx = dbFactory.GetContext();
        }

        public TObject Get(Guid id)
        {
            return Dbx.Set<TObject>().Find(id);
        }

        public IQueryable<TObject> GetAll()
        {
            return Dbx.Set<TObject>().AsQueryable();
        }

        public IQueryable<TObject> Where(Expression<Func<TObject, bool>> predicate)
        {
            return Dbx.Set<TObject>().Where(predicate);
        }

        public void Insert(TObject o)
        {
            Dbx.Set<TObject>().Add(o);
        }

        public void Save()
        {
            Dbx.SaveChanges();
        }

        public void Delete(TObject o)
        {
            Dbx.Set<TObject>().Remove(o);
        }
    }
}
